﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;

namespace ChrizNet.Log
{
    /// <summary>
    /// This class will log the exceptions raised in the application.
    /// </summary>
    public static class Logger
    {
        /// <summary>
        /// The constant for Event Viewer Source name
        /// </summary>
        private const String CHRIZ_NET = "ChrizNet";

        /// <summary>
        /// The suffix for log file.
        /// </summary>
        private const String CHRIZ_NET_LOGS = "ChrizNet Logs";

        private const String LOG_FILENAME = "Eventlog.txt";

        /// <summary>
        /// Constructor.
        /// </summary>
        static Logger()
        {
            if (!EventLog.SourceExists(CHRIZ_NET))
            {
                EventLog.CreateEventSource(CHRIZ_NET, CHRIZ_NET_LOGS);
                EventLog eventLog = new EventLog { Source = CHRIZ_NET };
                eventLog.ModifyOverflowPolicy(OverflowAction.OverwriteAsNeeded, 7);
            }
        }
        /// <summary>
        /// This methods logs the exception to Event Viewer.
        /// </summary>
        public static void Log(Exception e)
        {
            if (e != null)
            {
                Trace.WriteLine(e.ToString());
                EventLog.WriteEntry(CHRIZ_NET, e.ToString(), EventLogEntryType.Error);
            }
            //#if DEBUG
            //MessageBox.Show("An error has occurred. Please check Event Log for more details. ", LocalizationManager.Title);
            //MessageBox.Show(e.ToString());
            //#endif
        }

        /// <summary>
        /// This methods logs the passed String value in the Event Viewer.
        /// </summary>
        /// <param name="message">
        ///  It is the String value that will be logged.
        /// </param>
        public static void Log(String message)
        {
            String data = String.Format("{0} : {1}", "Information", message);
            Trace.WriteLine(data);
            EventLog eventLog = new EventLog(CHRIZ_NET_LOGS) { Source = CHRIZ_NET };
            eventLog.WriteEntry(data, EventLogEntryType.Information);
            eventLog.Close();
        }

        /// <summary>
        /// This methods logs the passed String value in the text file.
        /// </summary>
        /// <param name="message">
        ///  It is the String value that will be logged.
        /// </param>
        public static void LogToFile(String message)
        {
            System.DateTime curDateTime = System.DateTime.Now;
            String logPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), LOG_FILENAME);

            using (FileStream stream = new FileStream(logPath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
            {
                using (TextWriter writer = new StreamWriter(stream))
                {
                    writer.WriteLine(String.Format("{0}{1}{2} {3:00}:{4:00}:{5:00}:{6:00} - {7}",
                            curDateTime.Day, curDateTime.Month, curDateTime.Year, curDateTime.Hour, curDateTime.Minute, curDateTime.Second, curDateTime.Millisecond, message));

                    writer.Close();
                    writer.Dispose();
                }

                stream.Close();
                stream.Dispose();
            }

            //TextWriter textWriter = new StreamWriter(logPath, true);
            //textWriter.WriteLine(String.Format("{0}{1}{2} {3:00}:{4:00}:{5:00}:{6:00} - {7}",
            //        curDateTime.Day, curDateTime.Month, curDateTime.Year, curDateTime.Hour, curDateTime.Minute, curDateTime.Second, curDateTime.Millisecond, message));
            //textWriter.Close();
            //textWriter.Dispose();
        }

    }
}
